package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class MessageChangSendMessage extends ExecuteListener{
	
	public String getDescription() {
		return "信息变更审批结束，给申请人发企业微信消息及更新入住信息。";
	}

	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		// TODO Auto-generated method stub
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_SH_MESSAGECHANGE  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			List<Map<String , Object>> bodylist = DBSql.query("SELECT * FROM BO_EU_SH_MESSAGECHANGE_B  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String APPLYNAMEID=CoreUtil.objToStr(jcbd.get("APPLYNAMEID"));//申请人账号
			String APPLYUNIT=CoreUtil.objToStr(jcbd.get("APPLYUNIT"));//申请单位
			String APPLYNAME=CoreUtil.objToStr(jcbd.get("APPLYNAME"));//申请人
			String APPLYTELEPHONE=CoreUtil.objToStr(jcbd.get("APPLYTELEPHONE"));//申请人电话
			String ACCOUNTID=CoreUtil.objToStr(jcbd.get("ACCOUNTID"));//住宅号id
			String sqlchecktotall = "select INROOMNUM from BO_EU_SH_RCGY_ROOM t where  t.ID = '"
					+ ACCOUNTID + "'";
			String INROOMNUM = CoreUtil.objToStr(DBSql.getString(sqlchecktotall, "INROOMNUM"));// 现入住人数
			int numtotall ;
			if(!"".equals(INROOMNUM)) {
				numtotall =Integer.parseInt(INROOMNUM);
			}else {
				numtotall=0;
			}
			String sqlzlb = "select bindid from BO_EU_SH_RCGY_ROOM t where  t.id = '"
					+ ACCOUNTID + "'";
			String cardbindid = CoreUtil.objToStr(DBSql.getString(sqlzlb, "bindid"));// bindid
			//更新入住人员信息
			for (int i = 0; i < bodylist.size(); i++) {
				Map<String , Object> dataprobleminvent = bodylist.get(i);
				String CHANGETYPE = CoreUtil.objToStr(dataprobleminvent.get("CHANGETYPE"));// 变更类型修改|增加|删除
				String zlID = CoreUtil.objToStr(dataprobleminvent.get("FID"));// 租赁信息表id
				String workersname = CoreUtil.objToStr(dataprobleminvent.get("WORKERSNAME"));// 姓名
				String sex = CoreUtil.objToStr(dataprobleminvent.get("SEX"));// 性别
				String nation = CoreUtil.objToStr(dataprobleminvent.get("NATION"));// 民族
				String birthdate = CoreUtil.objToStr(dataprobleminvent.get("BIRTHDATE"));// 出生日期
				String idcard = CoreUtil.objToStr(dataprobleminvent.get("IDCARD"));// 身份证号
				String domicileadderss = CoreUtil.objToStr(dataprobleminvent.get("DOMICILEADDERSS"));// 户籍地址
				String telephone = CoreUtil.objToStr(dataprobleminvent.get("TELEPHONE"));// 联系电话
				String persontype = CoreUtil.objToStr(dataprobleminvent.get("PERSONTYPE"));// 人员类别
				if("修改".equals(CHANGETYPE)) {
					BO updatabody = new BO();
					updatabody.set("PSNTYPE",persontype);//人员类别
					updatabody.set("PSNNAME",workersname);//姓名
					updatabody.set("SEX",sex);//性别
					updatabody.set("BIRTHDATE",birthdate);//出生日期
					updatabody.set("NATION",nation);//民族
					updatabody.set("PSNCARDNO",idcard);//身份证号码
					updatabody.set("DOMICILEADDERSS",domicileadderss);//户籍地址
					updatabody.set("PSNPHONE",telephone);//手机号码
					updatabody.set("YUANGONGZH",APPLYNAMEID);//员工账号,新员工未建账号，先填充入住表ID
					updatabody.set("ID",zlID);
					SDK.getBOAPI().update("BO_EU_SH_RCGY_ROOM_PSN", updatabody);
				}else if("增加".equals(CHANGETYPE) && !"".equals(ACCOUNTID)){
					Date date = new Date();
					SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
					String todate = simpledateformat.format(date);
					List<BO> insertall = new ArrayList<BO>();//插入
					BO updatabody = new BO();
					updatabody.set("PSNTYPE",persontype);//人员类别
					updatabody.set("PSNNAME",workersname);//姓名
					updatabody.set("SEX",sex);//性别
					updatabody.set("BIRTHDATE",birthdate);//出生日期
					updatabody.set("NATION",nation);//民族
					updatabody.set("PSNCARDNO",idcard);//身份证号码
					updatabody.set("DOMICILEADDERSS",domicileadderss);//户籍地址
					updatabody.set("PSNPHONE",telephone);//手机号码
					updatabody.set("CHECKINTIME",todate);//入住时间
					updatabody.set("YUANGONGZH",APPLYNAMEID);//员工账号,新员工未建账号，先填充入住表ID
					insertall.add(updatabody);
					SDK.getBOAPI().create("BO_EU_SH_RCGY_ROOM_PSN", insertall, cardbindid, "admin");
					numtotall ++;
					
				}else if("删除".equals(CHANGETYPE)){
					SDK.getBOAPI().remove("BO_EU_SH_RCGY_ROOM_PSN", zlID);
					numtotall--;
				}
			}
			//更新入住人数字段
			String updatenosql = "update  BO_EU_SH_RCGY_ROOM set INROOMNUM = '"+numtotall+"' where id ='" + ACCOUNTID + "'";
			DBSql.update(updatenosql);
				
			//发送企业微信消息
			if(!"".equals(APPLYUNIT) && !"".equals(APPLYNAMEID) ) {
				String sqluserId ="select EXT5  from ORGUSER  where USERID='"+APPLYNAMEID+"'";
		        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
		        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+APPLYUNIT+"'";
		        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
		        if (jbData.size() > 0 && !"".equals(weixinid)) {
		        	String content="【人才公寓管理】您好！您申请办理的人员信息变更已办理完毕，如有增加或删除变更类型，请及时到人才公寓物业办理相关出入口人脸识别权限。";
		        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
					String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
					String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
					String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
					weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",content,0,agentid,acessToken);
		        }
			}
		}
	}
}
